<template>
  <div class="top-middle-cmp">
    <div class="chart-name">
      充值数据
      <dv-decoration-3 style="width:200px;height:20px;" />
    </div>
    <dv-charts :option="option" />
  </div>
</template>

<script>
import { paydata } from '@/api/chart'

export default {
  name: 'TopMiddleCmp',
  data() {
    return {
      option: {},
      recharges: [],
      times: [],
      dates: []
    }
  },
  mounted() {
    this.getChartData()
  },
  methods: {
    getChartData() {
      const query = { 'datetime': [new Date(Date.now() - 3600 * 1000 * 24 * 7), new Date()] }
      paydata(query).then(response => {
        this.handleData(response.data)
      })
    },
    handleData(data) {
      data.forEach(item => {
        this.recharges.push(parseInt(item.pay_amount))
        this.times.push(parseInt(item.pay_count))
        this.dates.push(item.date.substr(5))
      })
      console.log(this.recharges)
      this.setOpt()
    },
    setOpt() {
      this.option = {
        legend: {
          data: [
            {
              name: '充值人数',
              color: '#00baff'
            },
            {
              name: '充值金额',
              color: '#3de7c9'
            }
          ],
          textStyle: {
            fill: '#fff'
          }
        },
        xAxis: {
          data: this.dates,
          axisLine: {
            style: {
              stroke: '#999'
            }
          },
          axisLabel: {
            style: {
              fill: '#999'
            }
          },
          axisTick: {
            show: false
          }
        },
        yAxis: [
          {
            data: 'value',
            splitLine: {
              show: false
            },
            axisLine: {
              style: {
                stroke: '#999'
              }
            },
            axisLabel: {
              style: {
                fill: '#999'
              },
              formatter: '{value} 元'
            },
            axisTick: {
              show: false
            },
            min: 0
          }
        ],
        series: [
          {
            name: '充值人数',
            data: this.times,
            type: 'bar',
            barStyle: {
              fill: 'rgba(0, 186, 255, 0.4)'
            }
          },
          {
            name: '充值金额',
            data: this.recharges,
            type: 'line',
            smooth: true,
            label: {
              show: true,
              formatter: '{value}'
            },
            lineArea: {
              show: true,
              gradient: ['rgba(55, 162, 218, 0.6)', 'rgba(55, 162, 218, 0)']
            },
            lineStyle: {
              lineDash: [5, 5]
            },
            linePoint: {
              radius: 4,
              style: {
                fill: '#00db95'
              }
            }
          }
        ]
      }
    }
  }
}
</script>

<style lang="scss">
.top-middle-cmp {
  position: relative;
  padding: 0 50px;
  box-sizing: border-box;

  .chart-name {
    position: absolute;
    right: 70px;
    text-align: right;
    font-size: 20px;
    top: 10px;
  }
}
</style>
